var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
   iStap:false,
    catalogArray:[],
  //  lessons:['数学','绘画','奥数','钢琴','声乐','英语','语文','国学','创意画','儿童街舞','小升初'],
    lessons:[],
    // lessonType:'',
    grades: [
      {
        color: '#02a0e9',
        txt: '初一'
      },
      {
        color: '#313131',
        txt: '初二'
      },
      {
        color: '#313131',
        txt: '初三'
      }
    ],
    juniorHigh: ['语文', '数学', '英语', '历史', '政治', '地理', '生物'] ,
    highs: [
      {
        color: '#02a0e9',
        txt: '高一'
      },
      {
        color: '#313131',
        txt: '高二'
      },
      {
        color: '#313131',
        txt: '高三'
      }
    ],
    oneId:'',
    twoId:'',
    highSubejcts:['语文','数学','英语','物理','化学','生物','历史','地理','生物'],
    pageNo:2,
    tabdis:false,
    tableArray:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var index
    var sessionid
    let that = this
    wx.getStorage({
      key: 'sessionID',
      success: function(res) {
        console.log(res)
        sessionid=res.data
      },
    })
  
    wx.checkSession({
      success:function(res){
      // var tt=this
        // app.qingqiu('coursecenter/course', { pageNo: 1, pageSize: 10 }, '', that.callBackFun)
        var size,no
         size=10
         no=1
        wx.request({
          url: app.globalData.baseUrl+'coursecenter/course',
          data:{
            pageNo:no,
            pageSize:size
          },
          header:{
            'Cookie':'JSESSIONID='+sessionid
          },
          success:function(res){
           
          console.log(res)
            var length=res.data.data.data.length
            var all
             if(length<50){
                all=length
             }
            for (index = 0; index < all; index++) {
              var imgsrc = 'tableArray['+index+'].imgsrc'
              var id = 'tableArray[' + index + '].id'
              var title = 'tableArray[' + index + '].title'
              var description = 'tableArray[' + index + '].description'
              var sectionTotal = 'tableArray[' + index + '].sectionTotal'
              var description1=res.data.data.data[index].description
              if(description1.length>16){
                description1=description1.substring(0,15)+'...'
              }else{
                
              }

              that.setData({
                [imgsrc]: res.data.data.data[index].img,
                [id]: res.data.data.data[index].id,
                [title]: res.data.data.data[index].title,
                [description]: description1,
                [sectionTotal]: res.data.data.data[index].sectionTotal
              })
            }
            // wx.setStorage({
            //   key: 'sessionid',
            //   data: res.cookies[0].value,
            // })
            console.log(res)
          }
        })
      },
      fail:function(res){
        console.log('session失败')
        console.log(res)
      }
    })
    
  },
  
 
  toContent:function(event)
  { 
    
    console.log(event) 
    var id=event.currentTarget.dataset.id
   wx.navigateTo({
     url: '../content/content?id='+id,
   })
  },
  change:function(event)
  {
    var that=this
    //console.log(this.data.iStap)
    var iSlecte=this.data.iStap?false:true

    var tab=this.data.tabdis?false:true
     this.setData({
       iStap:!this.data.iStap,
      tabdis:!this.data.tabdis
     })
    //console.log(this.data.iStap)
    wx.request({
      url: app.globalData.baseUrl+'coursecenter/catalog',
      method:'get',
      data:{
        pageNo:1,
        pageSize:200
      },  
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },
      success:function(res){
        console.log(res)
        var resData=res.data.data.data
        for(var index=0;index<resData.length;index++){
          var id='catalogArray['+index+'].id'
          var name='catalogArray['+index+'].name'
          that.setData({
            [id]:resData[index].id,
            [name]:resData[index].name
          })
          // var twoCatalog='catalogArray['+index+'].twoCatalog'
          for(var i=0;i<resData[index].catalogTwoList.length;i++){
            // console.log(i)
            var idtwo = 'catalogArray[' + index +'].twoCatalog['+i+'].id'
            var nametwo = 'catalogArray[' + index + '].twoCatalog[' + i + '].name'
            that.setData({
              [idtwo]:resData[index].catalogTwoList[i].id,
              [nametwo]:resData[index].catalogTwoList[i].name
            })
          }
          
         }
      }

    })
  },
  chooseCatalogId:function(event){
    var that=this
    console.log(event)
    var oneId=event.currentTarget.dataset.id
    var twoId=event.detail.id
    console.log(oneId)
    console.log(twoId)
    this.setData({
      oneId:oneId,
      twoId:twoId
    })
    wx.request({
      url: app.globalData.baseUrl+'coursecenter/course',
      data:{
        catalogOneId:oneId,
        catalogTwoId:twoId
      },
      method:'get',
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },
      success:function(res){

        that.setData({
          tableArray:[],
          iStap: !that.data.iStap,
          tabdis: !that.data.tabdis
        })
        console.log(res)
        var resData=res.data.data.data
        if(resData.length==0){
          wx.showToast({
            title: '该目录没有数据',
          })
        }else{
          var length = res.data.data.data.length
          var all
          if (length < 50) {
            all = length
          }
          for (var index = 0; index < all; index++) {
            var imgsrc = 'tableArray[' + index + '].imgsrc'
            var id = 'tableArray[' + index + '].id'
            var title = 'tableArray[' + index + '].title'
            var description = 'tableArray[' + index + '].description'
            var sectionTotal = 'tableArray[' + index + '].sectionTotal'
            var description1 = res.data.data.data[index % 10].description
            if (description1.length > 16) {
              description1 = description1.substring(0, 15) + '...'
            } else {

            }

            that.setData({
              [imgsrc]: res.data.data.data[index % 10].img,
              [id]: res.data.data.data[index % 10].id,
              [title]: res.data.data.data[index % 10].title,
              [description]: description1,
              [sectionTotal]: res.data.data.data[index % 10].sectionTotal
            })
          }
        }
      },
      fail:function(err){
        wx.showToast({
          title: '获取数据错误',
          icon:'none',
          duration:4000
        })
        console.log(err)
      }
    })
  },
  getmoreLesson:function(pageNo){
    var pageNo=this.data.pageNo
    wx.request({
      url: app.globalData.baseUrl + 'coursecenter/course',
      data: {
        pageNo: pageNo,
        pageSize: 10
      },
      header: {
        'Cookie': 'JSESSIONID=' + sessionid
      },
      success: function (res) {

        console.log(res)
        var length = res.data.data.data.length
        var all
        if (length < 50) {
          all = length
        }
        for (index = pageNo*10; index < pageNo*10+all; index++) {
          var imgsrc = 'tableArray[' + index + '].imgsrc'
          var id = 'tableArray[' + index + '].id'
          var title = 'tableArray[' + index + '].title'
          var description = 'tableArray[' + index + '].description'
          var sectionTotal = 'tableArray[' + index + '].sectionTotal'
          var description1 = res.data.data.data[index%10].description
          if (description1.length > 16) {
            description1 = description1.substring(0, 15) + '...'
          } else {

          }

          that.setData({
            [imgsrc]: res.data.data.data[index%10].img,
            [id]: res.data.data.data[index%10].id,
            [title]: res.data.data.data[index%10].title,
            [description]: description1,
            [sectionTotal]: res.data.data.data[index%10].sectionTotal
          })
        }
        // wx.setStorage({
        //   key: 'sessionid',
        //   data: res.cookies[0].value,
        // })
        console.log(res)
      }
    })
  },
  //得到选择的返回的值
  switchRes:function(res)
  {
    var detail=res.detail.detail;
    if (detail == '初一') {
      this.setData({
        juniorHigh: ['语文', '数学', '英语', '历史', '政治', '地理', '生物'],
        grades: [
          {
            color: '#02a0e9',
            txt: '初一'
          },
          {
            color: '#313131',
            txt: '初二'
          },
          {
            color: '#313131',
            txt: '初三'
          }
        ]
      })
    } else if (detail == '初二') {
      this.setData({
        juniorHigh: ['语文', '数学', '英语', '历史', '政治', '地理', '生物','物理'],
        grades: [
          {
            color: '#313131',
            txt: '初一'
          },
          {
            color: '#02a0e9',
            txt: '初二'
          },
          {
            color: '#313131',
            txt: '初三'
          }
        ]
      })
    } else if (detail == '初三') {
      this.setData({
        juniorHigh: ['语文', '数学', '英语', '历史', '政治','物理','化学'],
        grades: [
          {
            color: '#313131',
            txt: '初一'
          },
          {
            color: '#313131',
            txt: '初二'
          },
          {
            color: '#02a0e9',
            txt: '初三'
          }
        ]
      })
    }
  },
  switchGrades:function(res)
  {
    var detail = res.detail.detail;
    if(detail=='高一'){
      this.setData({
        highs: [
          {
            color: '#02a0e9',
            txt: '高一'
          },
          {
            color: '#313131',
            txt: '高二'
          },
          {
            color: '#313131',
            txt: '高三'
          }
        ]
      })
    }else if(detail=='高二')
    {
      this.setData({
        highs: [
          {
            color: '#313131',
            txt: '高一'
          },
          {
            color: '#02a0e9',
            txt: '高二'
          },
          {
            color: '#313131',
            txt: '高三'
          }
        ]
      })
    }else if(detail=='高三')
    {
      this.setData({
        highs: [
          {
            color: '#313131',
            txt: '高一'
          },
          {
            color: '#313131',
            txt: '高二'
          },
          {
            color: '#02a0e9',
            txt: '高三'
          }
        ]
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (event) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // wx.showShareMenu({
    //   success:function(event){
    //     console.log(111)
    //   },
    //   fail:function(err){
    //     console.log('分享失败')
    //     console.log(err)
    //   }
    
    // })
    wx.request({
      url:app.globalData.baseUrl+'coursecenter/catalog',
      method:'GET',
      header:{
        'cookie':'JSESSIONID='+wx.getStorageSync('sessionID')
      },
      success:function(res){
        console.log(res)
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow()
    wx.showToast({
      icon: 'none',
      title: '刷新成功'
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('获取更多的课程')
    var pageNo=this.data.pageNo
    try{
      this.getmoreLesson(pageNo)
      this.setData({
        pageNo:this.data.pageNo+1
      })
    }catch(err){
      wx.showToast({
        title: '暂时没有更多的数据哦~~~',
        icon:'none'
      })
    }
   
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})